package com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.impl;

import com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.Translator;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRQ;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRS;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.ResponsePair;
import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailErrorDTO;
import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailRS;
import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailRoomStayDTO;
import com.derbysoft.dswitch.remote.hotel.dto.HotelAvailRequest;
import com.derbysoft.dswitch.remote.hotel.dto.HotelAvailResponse;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by: jason
 * Date: 2012-5-10
 */
@Component("hotelMultiAvailRSTranslator")
public class HotelMultiAvailRSTranslatorImpl extends BaseTranslator
        implements Translator<List<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>>, HotelAvailResponse, HotelAvailRequest> {

    @Override
    public HotelAvailResponse translate(List<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>> otaHotelAvailRSes, HotelAvailRequest request) {
        HotelAvailResponse hotelAvailResponse = new HotelAvailResponse();
        hotelAvailResponse.setTaskId(request.getHeader().getTaskId());
        hotelAvailResponse.setHotelAvailRS(createHotelAvailRS(otaHotelAvailRSes, request));
        return hotelAvailResponse;
    }

    private HotelAvailRS createHotelAvailRS(List<ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS>> availResponsePairs, HotelAvailRequest request) {
        HotelAvailRS hotelAvailRS = new HotelAvailRS();
        List<HotelAvailRoomStayDTO> hotelAvailRoomStayDTOs = new ArrayList<HotelAvailRoomStayDTO>();
        List<String> noAvailHotels = new ArrayList<String>();
        List<HotelAvailErrorDTO> hotelAvailErrorDTOs = new ArrayList<HotelAvailErrorDTO>();
        for (ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS> availResponsePair : availResponsePairs) {
            if (availResponsePair.getResponse().getErrors() != null) {
                if (availResponsePair.getResponse().getErrors().getError().get(0).getShortText().contains(NO_AVAIL_TEXT)) {
                    noAvailHotels.add(availResponsePair.getRequest().getHotelPassport());
                } else {
                    hotelAvailErrorDTOs.add(createAvailError(availResponsePair));
                }
            } else {
                hotelAvailRoomStayDTOs.add(createAvailRoomStayDTO(availResponsePair, request));
            }
        }

        hotelAvailRS.setNoAvailHotelsList(noAvailHotels);
        hotelAvailRS.setHotelAvailErrorsList(hotelAvailErrorDTOs);
        hotelAvailRS.setHotelAvailRoomStaysList(hotelAvailRoomStayDTOs);
        return hotelAvailRS;
    }

    private HotelAvailErrorDTO createAvailError(ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS> availResponsePair) {
        HotelAvailErrorDTO hotelAvailErrorDTO = new HotelAvailErrorDTO();
        hotelAvailErrorDTO.setHotelCode(availResponsePair.getRequest().getHotelPassport());
        hotelAvailErrorDTO.setErrorSource(provider);
        hotelAvailErrorDTO.setErrorCode(availResponsePair.getResponse().getErrors().getError().get(0).getCode());
        hotelAvailErrorDTO.setErrorMsg(availResponsePair.getResponse().getErrors().getError().get(0).getShortText());
        return hotelAvailErrorDTO;
    }

    private HotelAvailRoomStayDTO createAvailRoomStayDTO(ResponsePair<OTAHotelAvailRQ, OTAHotelAvailRS> availResponsePair, HotelAvailRequest request) {
        HotelAvailRoomStayDTO hotelAvailRoomStayDTO = new HotelAvailRoomStayDTO();
        hotelAvailRoomStayDTO.setHotelCode(availResponsePair.getRequest().getHotelPassport());
        hotelAvailRoomStayDTO.setRoomStaysList(createRoomStays(availResponsePair.getResponse(), request));
        return hotelAvailRoomStayDTO;
    }
}
